package com.ruoyi.procurementmanagement.domain;

import com.ktjiaoyu.erpcommon.domain.ErpWarehouseStock;
import com.ktjiaoyu.erpcommon.service.IErpWarehouseStockService;
import com.ruoyi.procurementmanagement.service.IEsProcurementPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author 曹正
 * @Date 2025/6/13 8:59
 **/
// 创建定时任务类
@Component
public class AutoReplenishmentTask {

    @Autowired
    private IErpWarehouseStockService warehouseStockService;

    @Autowired
    private IEsProcurementPlanService procurementPlanService;

    // 每天凌晨1点执行
    // 修改后的 AutoReplenishmentTask.java 中的 autoGenerateReplenishmentPlan 方法
    @Scheduled(cron = "0 0 1 * * ?")
    public void autoGenerateReplenishmentPlan() {
        // 1. 查询需要补货的商品
        List<ErpWarehouseStock> warningItems = warehouseStockService.selectWarningStockList();

        if (warningItems.isEmpty()) {
            return;
        }

        // 2. 创建补货计划
        EsProcurementPlan plan = new EsProcurementPlan();
        plan.setSn(generatePlanNumber());
        plan.setFormationTime(new Date());
        plan.setProcurementDesc("定时任务自动生成的补货计划");
        plan.setDeptId(4); // 默认采购部门ID
        plan.setDeptName("采购部");
        plan.setFormationPersonId(1); // 默认管理员ID
        plan.setFormationPerson("管理员");
        plan.setSupplierId(1); // 默认供应商ID
        plan.setSupplierName("默认供应商");

        // 3. 添加补货明细
        List<EsProcurementPlanProduct> products = warningItems.stream()
                .map(item -> {
                    EsProcurementPlanProduct product = new EsProcurementPlanProduct();
                    product.setGoodsId(item.getGoodsId());
                    product.setGoodsName(item.getGoodsName());
                    product.setGoodsSn(item.getGoodsSn());
                    product.setSpecification(item.getProductSpecification());
                    product.setCategoryId(item.getCategoryId()); // 需要确保ErpWarehouseStock中有这个字段
                    product.setCategoryName(item.getCategoryName()); // 需要确保ErpWarehouseStock中有这个字段
                    product.setProcurementNum(calculateQuantity(item));
                    product.setSupplyTime(new Date()); // 默认供应时间为当前时间
                    product.setRemark("自动补货计划生成");
                    product.setCostPrice(item.getCostPrice()); // 需要确保ErpWarehouseStock中有这个字段
                    product.setTaxRate(item.getTaxRate()); // 需要确保ErpWarehouseStock中有这个字段
                    return product;
                })
                .collect(Collectors.toList());

        plan.setProductList(products);

        // 4. 保存补货计划
        procurementPlanService.insertEsProcurementPlan(plan);
    }

    // 修改后的 calculateQuantity 方法
    private int calculateQuantity(ErpWarehouseStock item) {
        int currentStock = item.getInQuantity() - item.getOutQuantity();
        int safetyStock = item.getWarningValue() != null ? item.getWarningValue() : 10; // 使用商品预警值或默认值
        return Math.max(safetyStock - currentStock, 1); // 至少补货1个
    }

    private String generatePlanNumber() {
        // 生成计划编号逻辑
        return "PL" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
}
